using System;
using System.Diagnostics;

namespace XTCast.Mp3 {
	public class Log {

		static TraceSwitch all = new TraceSwitch("All", "Everything");
		static TraceSwitch local = new TraceSwitch("XTCast.Mp3", "Local DLL");

		static public void WriteLine(String msg) {
			Timestamp();
			Trace.WriteLine(msg);
		}

		static public void WriteLine(Object who,String msg) {
			if (who!=null)
				Trace.WriteLine(who.ToString() + ": " + msg);
			else
				Trace.WriteLine(msg);
		}

		static public void Error(String msg) {
			if (all.TraceError || local.TraceError)
				WriteLine(null,msg);
		}

		static public void Error(Exception ex) {
			if (all.TraceError || local.TraceError)
				WriteLine(null,ex.ToString());
		}

		static public void Error(Object who, Exception ex) {
			if (all.TraceError || local.TraceError)
				WriteLine(who,ex.ToString());
		}

		static public void Error(Object who, String msg) {
			if (all.TraceError || local.TraceError)
				WriteLine(who,msg);
		}

		static public void Warn(String msg) {
			if (all.TraceWarning || local.TraceWarning)
				WriteLine(null,msg);
		}

		static public void Warn(Object who, String msg) {
			if (all.TraceWarning || local.TraceWarning)
				WriteLine(who,msg);
		}

		static public void Info(String msg) {
			if (all.TraceInfo || local.TraceInfo)
				WriteLine(null,msg);
		}

		static public void Info(Object who, String msg) {
			if (all.TraceInfo || local.TraceInfo)
				WriteLine(who,msg);
		}

		static public void Verbose(String msg) {
			if (all.TraceVerbose || local.TraceVerbose)
				WriteLine(null,msg);
		}

		static public void Verbose(Object who, String msg) {
			if (all.TraceVerbose || local.TraceVerbose)
				WriteLine(who,msg);
		}

		
		/// <summary>How often (in millis) timestamps are written to the log </summary>
		private const long kStampInterval = (long)5 * 60 * 1000 * 10000; // 5 minutes
		private static long sLastStamp = 0;	

		/// <summary>Writes a time-stamp if it's been long enough since the last one was written. </summary>
		private static void Timestamp() {
			long now = (System.DateTime.Now.Ticks);
			if (now - sLastStamp > kStampInterval) {
				sLastStamp = now;
				WriteLine("____" + (new System.DateTime(now)).ToString("r") + "____");
			}
		}
	}	
}